package pers.bbw.crr.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import pers.bbw.crr.dto.UserSearchDto;
import pers.bbw.crr.entity.User;
import pers.bbw.crr.service.UserService;
import pers.bbw.crr.util.AjaxResult;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

/**
 * 用户信息控制器
 *
 * @author bbw
 * @date 2024-12-13
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户信息控制器")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 查询用户列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "列表", notes = "分页查询")
    public AjaxResult list(@ApiParam(value = "用户信息searchDto") UserSearchDto dto) {
        return AjaxResult.success(userService.selectUserList(dto));
    }

    /**
     * 获取用户详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "详情", notes = "根据id查询")
    public AjaxResult getInfo(@ApiParam(value = "用户id") @PathVariable("id") int id) {
        return AjaxResult.success(userService.selectUserById(id));
    }

    /**
     * 新增用户
     */
    @PostMapping
    @ApiOperation(value = "新增用户信息")
    public AjaxResult add(@ApiParam(value = "用户信息") User user, MultipartFile file) {
        userService.insertUser(user, file);
        return AjaxResult.success();
    }

    /**
     * 修改用户
     */
    @PutMapping
    @ApiOperation(value = "修改")
    public AjaxResult edit(@ApiParam(value = "用户信息") User user, MultipartFile file) {
        userService.updateUser(user, file);
        return AjaxResult.success();
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "批量删除")
    public AjaxResult remove(@ApiParam(value = "用户id数组") @PathVariable int[] ids) {
        return AjaxResult.success(userService.deleteUserByIds(ids));
    }

    @GetMapping("/captcha")
    @ApiOperation(value = "获取登录验证码", notes = "白博文 0205")
    public AjaxResult Captcha(HttpServletRequest request) throws IOException {
        return AjaxResult.success(userService.captcha(request));
    }

    @PostMapping("/register")
    @ApiOperation(value = "注册", notes = "白博文 0205")
    public AjaxResult Register(@ApiParam(value = "用户信息") User user, MultipartFile file) {
        if (userService.register(user, file)) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error("该账号已被注册，请更换账号注册");
        }
    }

    @PostMapping("/login")
    @ApiOperation(value = "用户登录", notes = "白博文 0205")
    public AjaxResult userLogin(@ApiParam(value = "账号信息") @RequestBody Map<String, String> loginInfo, HttpServletRequest request) {
        int resultCode = userService.loginCheck(loginInfo, request);
        if (resultCode == 0) {
            return AjaxResult.error("账号密码错误，请重试");
        } else if (resultCode == 1) {
            return AjaxResult.error("验证码已失效");
        } else if (resultCode == 2) {
            return AjaxResult.error("验证码错误，请刷新重试");
        } else {
            return AjaxResult.success("登录成功");
        }
    }

}
